<template>
  <lay-container fluid="true" style="padding: 10px">
    <lay-row :space="10">
      <lay-col :md="24">
        <lay-card>
          <lay-table :page="page" :columns="columns" :dataSource="dataSource" 
            v-model:selectedKeys="selectedKeys" @change="change" :size="tablesize">
            <template v-slot:toolbar>
              <lay-button type="primary" size="sm" @click="add" v-permission="PerKey.UserGroup_AddOrEditView"
                prefix-icon="layui-icon-addition">新增
              </lay-button>
            </template>
            <template v-slot:operator="{ data }">
              <lay-button-group>
                <lay-button type="primary" size="sm" @click="edit(data.id)"
                  v-permission="PerKey.UserGroup_AddOrEditView" prefix-icon="layui-icon-edit">编辑
                </lay-button>
                <lay-button type="danger" size="sm" @click="del(data)" prefix-icon="layui-icon-delete"
                  v-permission="PerKey.UserGroup_Delete">删除
                </lay-button>
              </lay-button-group>
            </template>
          </lay-table>
        </lay-card>
      </lay-col>
    </lay-row>

    <lay-layer :title="addoredittip" v-model="modelVisible" :resize="true" :area="['1200px', '800px']">
      <lay-card style="padding:10px;">
        <lay-row space="10">
          <lay-form :model="form">
            <lay-col :md="24">
              <lay-form-item label="角色名称" prop="groupName" required required-error-message="角色名称不能空">
                <lay-col md="12">
                  <lay-input v-model="form.groupName"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
            <lay-col :md="24">
              <lay-form-item label="备注" prop="remark">
                <lay-col md="12">
                  <lay-input v-model="form.remark"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
            <lay-col :md="24">
              <lay-form-item label="菜单权限" prop="ContentCateRole">
                <lay-tree :data="treeData" v-model:checkedKeys="checkedKeys" :showCheckbox="true" collapse-transition
                  :showLine="true" class="qy-tree">
                </lay-tree>
              </lay-form-item>
            </lay-col>
            <lay-col :md="24" class="saveBtn">
              <lay-button @click="submitClick" type="primary" v-permission="PerKey.UserGroup_AddOrEdit"
                prefix-icon="layui-icon-ok">
                {{ addoredittip }}
              </lay-button>
            </lay-col>
          </lay-form>
        </lay-row>
      </lay-card>
    </lay-layer>
  </lay-container>
</template>
  
<script lang="ts" setup>
import { reactive, ref, toRefs, watch } from "vue";
import { layer } from "@layui/layer-vue";
import { getList, delOne, getEdit, putEdit, postAdd } from "../../../api/module/user/usergroup";
import { PerKey } from "../../../library/permissionConst"
import { UserGroup } from '../../../types/user'
const selectedKeys = ref(["1"]);
const page = ref({ total: 0, limit: 10, current: 1, showRefresh: true, });
const tablesize = ref('md');
const columns = [
  { title: "Id", width: "50px", key: "id" },
  { title: "用户组", width: "220px", key: "groupName", },
  { title: "备注", width: "320px", key: "remark", },
  { title: "操作", customSlot: "operator", key: "operator", align: "left", }
]
const dataSource = ref([])

const GetList = function (current = page.value.current, limit = page.value.limit) {
  getList(current, limit).then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.data
      page.value.total = res.data.count
    }
  })
}

const change = function ({ current, limit }: any) {
  GetList(current, limit);
};

const edit = function (id: number) {
  changeVisible(id)
}
const add = function () {
  changeVisible(0)
}

const del = function (data: any) {
  layer.confirm("确认删除角色: " + data.groupName + " 吗？", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        delOne(data.id).then((res) => {
          if (res.status == 1) {
            layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
              GetList()
            });
          } else {
            layer.msg(res.msg);
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}


const modelVisible = ref(false)
const data = reactive({
  form: <UserGroup><unknown>[]
})
const { form } = toRefs(data)
let addoredittip = ref();
const treeData = ref([])
const checkedKeys = ref([])
const changeVisible = function (id: number) {
  modelVisible.value = !modelVisible.value
  getEdit(id).then((res) => {
    if (res.status == 1) {
      treeData.value = res.data.menuList
      if (id == 0) {
        addoredittip.value = "新增角色";
        form.value = {
          id: 0,
          groupName: "",
          systemRole: "",
        }
        return
      } else {
        addoredittip.value = "编辑角色";
        let data = res.data.data
        form.value = data
        let cate = JSON.parse(data.systemRole ?? null)
        if (cate != null) {
          var turnNum = function (cate: any) {
            return cate.map(Number);
          }
          checkedKeys.value = turnNum(cate)
        }
        return
      }
    } else {
      layer.msg("获取信息失败")
    }
  })
}

const submitClick = function () {
  if (!form.value.groupName) {
    layer.msg("角色名称不能空", { time: 2000, icon: 2 })
    return
  }
  form.value.systemRole = JSON.stringify(checkedKeys.value)
  if (form.value.id == 0) {
    postAdd(form.value).then((res) => {
      done(res)
    });
  } else {
    putEdit(form.value).then((res) => {
      done(res)
    });
  }
};
const done = (res: any) => {
  if (res.status == 1) {
    layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
      modelVisible.value = !modelVisible.value
      GetList()
    });
  }
}
GetList()
</script>
   
<style>
.qy-tree .layui-tree-set .layui-tree-pack .layui-tree-set .layui-tree-set {
  display: inline-block !important;
  width: 180px !important;
}
</style>